package com.yotta.operationdatascreencode.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yotta.operationdatascreencode.base.entity.TestUserEntity;
import com.yotta.operationdatascreencode.base.mapper.TestUserMapper;
import com.yotta.operationdatascreencode.common.core.BasePageResult;
import com.yotta.operationdatascreencode.common.exception.BusinessException;
import com.yotta.operationdatascreencode.common.utils.UidUtils;
import com.yotta.operationdatascreencode.constants.TestUserExceptionConstants;
import com.yotta.operationdatascreencode.web.service.TestUserService;

/**
 * 测试用户逻辑层
 */
public class TestUserServiceImpl extends ServiceImpl<TestUserMapper, TestUserEntity>
    implements TestUserService {

    @Override
    public BasePageResult<TestUserEntity> list(Long curPage, Long pageSize, String name) {

        LambdaQueryWrapper<TestUserEntity> wrapper = new LambdaQueryWrapper<TestUserEntity>()
            .eq(StrUtil.isNotBlank(name), TestUserEntity::getName, name)
            .isNull(TestUserEntity::getDeletedAt).orderByDesc(TestUserEntity::getCreatedAt);
        IPage<TestUserEntity> pageInfo = this.baseMapper.selectPage(
            new Page<TestUserEntity>(curPage,pageSize), wrapper);
        if (ObjectUtil.isNull(pageInfo) || ObjectUtil.isEmpty(pageInfo)) {
            throw new BusinessException(TestUserExceptionConstants.PAGE_LISGT_ERROR);
        }
        BasePageResult<TestUserEntity> pageResult = BasePageResult.newInstance(pageInfo);
        return pageResult;
    }

    /**
     * 保存
     * @param name 用户名
     * @return 结果集
     */
    @Override
    public int save(String name) {
        TestUserEntity user = new TestUserEntity();
        user.setId(UidUtils.generateUid());
        user.setName(name);
        this.baseMapper.insert(user);
        return 0;
    }
}
